package org.maxgroundstation.groundstation.view.map;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;

import org.maxgroundstation.world.landmark.Landmark;

/**
 * A dialog for displaying landmark data.
 * @author Scott Davis
 * 
 * Copyright (C) 2012  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
@SuppressWarnings("serial")
public class LandmarkDialog extends JDialog {
    
    /**
     * Constructor
     * @param owner the JFrame dialog owner.
     * @param landmark the landmark.
     */
    public LandmarkDialog(JFrame owner, Landmark landmark) {
        // User JDialog constructor.
        super(owner, landmark.getName(), false);
        
        // Set the layout.
        setLayout(new BorderLayout());
        
        // Create the top panel.
        JPanel topPanel = new JPanel(new BorderLayout(0, 0));
        add(topPanel, BorderLayout.NORTH);
        
        // Create the info panel.
        JPanel infoPanel = new JPanel(new GridLayout(2, 1));
        topPanel.add(infoPanel, BorderLayout.SOUTH);
        
        DecimalFormat positionFormatter = new DecimalFormat("0.000000");
        
        // Create the latitude panel.
        JPanel latitudePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        infoPanel.add(latitudePanel);
        
        // Create the latitude label.
        JLabel latitudeLabel = new JLabel("Latitude: " + positionFormatter.format(landmark.getLatitude()));
        latitudePanel.add(latitudeLabel);
        
        // Create the longitude panel.
        JPanel longitudePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        infoPanel.add(longitudePanel);
        
        // Create the longitude label.
        JLabel longitudeLabel = new JLabel("Longitude: " + positionFormatter.format(landmark.getLongitude()));
        longitudePanel.add(longitudeLabel);
        
        // Create the data panel.
        JPanel dataPanel = new JPanel(new BorderLayout());
        add(dataPanel, BorderLayout.CENTER);
        
        // Create the name panel.
        JPanel namePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        dataPanel.add(namePanel, BorderLayout.NORTH);
        
        // Create the name label.
        JLabel nameLabel = new JLabel("Name: " + landmark.getName());
        namePanel.add(nameLabel);
        
        // Create the description panel.
        JPanel descriptionPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        descriptionPanel.setBorder(new TitledBorder("Description"));
        dataPanel.add(descriptionPanel, BorderLayout.CENTER);
        
        // Create the description text area.
        JTextArea descriptionTA = new JTextArea("", 10, 30);
        descriptionTA.setEditable(false);
        descriptionTA.setText(landmark.getDescription());
        descriptionPanel.add(descriptionTA);
        
        // Create the button panel.
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        add(buttonPanel, BorderLayout.SOUTH);
        
        // Create the close button.
        JButton cancelButton = new JButton("Close");
        cancelButton.setToolTipText("Close the dialog.");
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        buttonPanel.add(cancelButton);
        
        // Pack the dialog.
        pack();
        
        // Set the location of the dialog.
        int xPos = owner.getX() + (owner.getWidth() / 2) - (getWidth() / 2);
        int yPos = owner.getY() + (owner.getHeight() / 2) - (getHeight() / 2);
        setLocation(new Point(xPos, yPos));
    }
}